home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / SprocketExamples / GlyphaIV / GlyphaIV Sources / G4Lava.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-14  |  3.0 KB  |  159 lines  |  [TEXT/CWIE]

  1. #include "G4Externs.h"
  2.  
  3. #define    bufWidth 640
  4. #define bufHeight 60
  5. #define    vWidth bufWidth
  6. #define    vHeight bufHeight-6
  7. #define MysteryNumber 12
  8. extern CGrafPtr    workSrcMap;
  9.  
  10. unsigned char * fireBuffer = nil; // [bufWidth][bufHeight];// VGA buffer, quarter resolution w/extra lines
  11.  
  12. void DrawLava(void);
  13. void HandleLava(void);
  14. void InitFire(void);
  15.  
  16. void DrawLava(void)
  17. {
  18.     CGrafPtr theGrafPtr = workSrcMap;
  19.     PixMap *theMap = *(theGrafPtr->portPixMap);
  20.     unsigned long rowBytes = theMap->rowBytes & 0x3fff;
  21.     unsigned char *screen = (unsigned char *) theMap->baseAddr;
  22.     int            i;
  23.     int            j;
  24.     unsigned char * index;
  25.        unsigned char *currentLine = screen + (((480 + MysteryNumber) - vHeight) * rowBytes);
  26.     
  27.     index = fireBuffer;
  28.     
  29.     for(i=0; i < vHeight; i++)
  30.     {
  31.         unsigned char *fireIndex = index;
  32.         
  33.         for(j=0; j < vWidth; j++)
  34.         {
  35.             // unsigned char thisPixel = Convert(*fireIndex);
  36.             unsigned char thisPixel = ((*fireIndex) & 0x00f0) >> 3;
  37.             
  38.             if (thisPixel != 0)
  39.             {    
  40.             unsigned char ConversionTable[32] = {    255, 255,
  41.                                                 255, 223, 
  42.                                                 222, 221,
  43.                                                 220, 219,
  44.                                                 218, 217,
  45.                                                 216, 215,
  46.                                                 35, 29,
  47.                                                 22, 23,
  48.                                                 16, 17,
  49.                                                 10, 11,
  50.                                                 5, 5, 
  51.                                                 4, 4,
  52.                                                 3, 3, 
  53.                                                 2, 2,
  54.                                                 1, 1,
  55.                                                 0, 0};
  56.     
  57.     
  58.                 thisPixel = ConversionTable[thisPixel];
  59.                 // thisPixel = 0;
  60.                 
  61.                 currentLine[j] = thisPixel;    
  62.             }        
  63.     
  64.             fireIndex++;
  65.         }
  66.         
  67.         currentLine += rowBytes;
  68.         index += bufWidth;
  69.     }
  70.  
  71. }
  72.  
  73. void HandleLava(void)
  74. {
  75. int        i;
  76. int        j;
  77. int        delta;
  78. unsigned char FireModTable[] = { 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  79.  
  80. register unsigned char *pix;
  81. unsigned char *left,*top,*right, *pixToSet;
  82.  
  83.     if (fireBuffer == nil)
  84.     {
  85.         InitFire();
  86.     }
  87.     
  88.     // Transform current buffer
  89.     for(i=1; i<bufHeight-1; i++)
  90.     {                
  91.         pix = fireBuffer + (bufWidth * i);
  92.         pixToSet = pix - bufWidth; //row above pix
  93.  
  94.         for(j=0 ;j<bufWidth ;j++)
  95.         {
  96.  
  97.  
  98.             if(j==0) 
  99.             {
  100.                 //Init these variables first time through row loop
  101.                 left = fireBuffer + (bufWidth*i);  //fireBuffer[i][0] not used until next time through for 
  102.  
  103.                 right = left+1 ; //fireBuffer[i][1] 
  104.                 top = left+bufWidth; //&p1[i+1][0]
  105.  
  106.                 *pixToSet = (*pix + *(pixToSet+79) + *right + *top) / 4; //p1[i-1][79]
  107.  
  108.                 top++; right++; pixToSet++;
  109.             }
  110.             else if(j== (bufWidth-1)) 
  111.                 *pixToSet = (*pix + *left + *(pixToSet+1)+ *top ) / 4; //p1[i+1][0]
  112.             else 
  113.                 {
  114.                 *pixToSet = (*pix + *left + *right + *top ) / 4;
  115.                 left++; right++; top++; pixToSet++;
  116.                 }
  117.     
  118.         if (*pix > 11)
  119.             *pix = *pix - 12;
  120.         else
  121.             *pix = FireModTable[*pix];
  122.  
  123.         pix++;
  124.         }
  125.     }
  126.  
  127.  
  128.     // Set new bottom line with random white or black
  129.     delta = 0;
  130.  
  131.     top = fireBuffer + ((bufHeight -2) * bufWidth);
  132.     left = top + bufHeight;
  133.  
  134.     for(j=0;j<bufWidth;j++)
  135.     {
  136.         //change this time?
  137.         if (Random() < 0) 
  138.         {
  139.             if (Random() > 0)
  140.                 delta = 0;
  141.             else
  142.                 delta = 255;
  143.         }
  144.         *top++ = delta;
  145.         *left++ = delta;
  146.     }
  147. }
  148.  
  149.  
  150. // workSrcMap
  151. void InitFire(void)
  152. {    
  153.     fireBuffer = (unsigned char *)NewPtrClear (bufHeight*bufWidth);
  154.     
  155.     // NOTE: ??? screen = screen+ (52 + (52*rowBytes));
  156.  }
  157.  
  158.  
  159.